# Contributor: Pedro Filipe <xpecex@outlook.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=mosquitto
pkgver=2.0.20
pkgrel=0
pkgdesc="open source MQTT broker"
url="https://mosquitto.org/"
arch="all"
license="EPL-1.0 OR EPL-2.0"
replaces="mosquitto-utils"
install="$pkgname.pre-install"
options="!check" # FIXME: some of the tests are flaky
makedepends="openssl-dev>3 c-ares-dev util-linux-dev libxslt"
checkdepends="python3 cunit-dev"
subpackages="$pkgname-dev $pkgname-doc $pkgname-libs++:_pp $pkgname-openrc
	$pkgname-libs $pkgname-clients"
source="https://mosquitto.org/files/source/mosquitto-$pkgver.tar.gz
	mosquitto.initd
	mosquitto.confd
	"

if [ -z "$BOOTSTRAP" ] && [ -z "$APORTS_BOOTSTRAP" ]; then
	subpackages="$pkgname-dbg $subpackages"
	makedepends="$makedepends libwebsockets-dev cjson-dev"
	_websockets=yes
	_cjson=yes
else
	_websockets=no
	_cjson=no
fi

# secfixes:
#   2.0.16-r0:
#     - CVE-2023-28366
#     - CVE-2023-0809
#     - CVE-2023-3592
#   2.0.10-r0:
#     - CVE-2021-28166
#   2.0.8-r0:
#     - CVE-2021-34432
#   1.6.7-r0:
#     - CVE-2019-11779
#   1.5.6-r0:
#     - CVE-2018-12546
#     - CVE-2018-12550
#     - CVE-2018-12551
#   1.5.3-r0:
#     - CVE-2018-12543
#   1.4.15-r0:
#     - CVE-2017-7652
#     - CVE-2017-7651
#   1.4.13-r0:
#     - CVE-2017-9868
#   1.4.12-r0:
#     - CVE-2017-7650

build() {
	# PSK not supported by libressl
	make \
		WITH_MEMORY_TRACKING=no \
		WITH_WEBSOCKETS=$_websockets \
		WITH_CJSON=$_cjson \
		WITH_SRV=yes \
		WITH_ADNS=no \
		prefix=/usr
}

check() {
	make test
}

package() {
	make prefix=/usr \
		DESTDIR="$pkgdir" \
		WITH_CJSON=$_cjson \
		install

	mv "$pkgdir"/etc/mosquitto/mosquitto.conf.example \
		"$pkgdir"/etc/mosquitto/mosquitto.conf
	sed -i -e 's/#log_dest stderr/log_dest syslog/' \
		"$pkgdir"/etc/mosquitto/mosquitto.conf

	install -Dm755 "$srcdir"/mosquitto.initd "$pkgdir"/etc/init.d/mosquitto
	install -Dm644 "$srcdir"/mosquitto.confd "$pkgdir"/etc/conf.d/mosquitto
}

_pp() {
	pkgdesc="C++ wrapper for libmosquitto"
	replaces=""

	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libmosquittopp.so.* "$subpkgdir"/usr/lib/
}

clients() {
	pkgdesc="Mosquitto command line MQTT clients"
	replaces="mosquitto-utils"

	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/mosquitto_[ps]ub "$subpkgdir"/usr/bin/
}

sha512sums="
4c16ff0ad0e9c6fe5c65ec5a82bef123d9e6490f42658d646e1fad60f098530b7cbbd743460c52d4dec8bc27698c84a557bd539b35ac6c81444539822a544c4c  mosquitto-2.0.20.tar.gz
a527813957b6f2d7afdb7269bade61d99b3023a147861b38902971929ff342a7c8c276bdb808fcfe7e48fa3e5c7521a16d777e5a3313256b8bf1e759cec5b7b0  mosquitto.initd
678a8aaefb9181f5f4998304046e5a8737049f90cf6bbbfd5fd4549592728afe77cb536547b39ad1598d53fe0b7c03e1506b2683e7b936712b9fad4a317f4b43  mosquitto.confd
"
